package cn.com.newloading.config;

import cn.com.newloading.common.utils.mq.MQConnectionUtil;
import cn.com.newloading.common.utils.mq.MQMsgUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author qja
 * @date 2021-2021/5/15-20:00
 * @describe
 */
public class MQInitConfig {
    private static Logger logger = LoggerFactory.getLogger(MQInitConfig.class);

    /**
     * 初始化接收信息
     * @throws IOException
     * @throws TimeoutException
     */
    public void initConfig() throws IOException, TimeoutException {
        simpleQueueRecv();
        workQueueRecv1();
    }

    /**
     * 简单队列接收消息
     * @throws IOException
     * @throws TimeoutException
     */
    public void simpleQueueRecv() throws IOException, TimeoutException {
        Connection connection = MQConnectionUtil.getConnection(); //获取连接
        Channel channel = connection.createChannel();//从连接中创建通道
        //声明队列
        channel.queueDeclare(MQMsgUtil.SIMPLE_QUEUE,false,false,false,null);
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            private String msg;
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String recvMsg = new String(body,"utf-8");
                logger.info("============recv message success:" + recvMsg);
            }
        };
        //监听队列
        //basicConsume(String queue, boolean autoAck, Consumer callback)
        //autoAck 是否自动确认
        channel.basicConsume(MQMsgUtil.SIMPLE_QUEUE,true,defaultConsumer);
    }

    /**
     * 工作队列接收消息
     * @throws IOException
     * @throws TimeoutException
     */
    public void workQueueRecv1() throws IOException, TimeoutException {
        Connection connection = MQConnectionUtil.getConnection(); //获取连接
        Channel channel = connection.createChannel();//从连接中创建通道
        //声明队列
        channel.queueDeclare(MQMsgUtil.WORK_QUEUE_1,false,false,false,null);
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            private String msg;
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String recvMsg = new String(body,"utf-8");
                logger.info("============recv message success:" + recvMsg);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        //监听队列
        //basicConsume(String queue, boolean autoAck, Consumer callback)
        //autoAck 是否自动确认
        channel.basicConsume(MQMsgUtil.WORK_QUEUE_1,true,defaultConsumer);
    }
}
